package App.Job;

/**
 * Created by xw on 2019/5/28.
 */

import App.Model.Account.CrmUser;
import App.Model.JPush.JPushCustomerContent;
import App.Model.Task.CrmTask;
import App.Model.Task.CrmTaskMember;
import App.Model.Task.CrmTaskProjectRelation;
import App.Repository.FriendRepository;
import App.Repository.TaskRepository;
import App.Service.JPushService;
import App.Service.XingeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

/**
 * 24小时没有完成的task，发送提醒
 */


@Component
public class Task24HourJob {


    @Autowired
    private TaskRepository _taskRepository;

    @Autowired
    private FriendRepository _friendRepository;

    @Autowired
    private XingeService _xingeService;

    @Autowired
    private JPushService _jPushService;

    public void execute() {

        System.out.println("the 24 hour job ");

        try {
            String _messageTemplate = "请尽快完成本任务";
            List<CrmTask> _tasks = _taskRepository.getTasks24Hours();
            for (CrmTask t : _tasks) {
                String _taskid = t.getRecid();
                String _taskContent = t.getContent();

                SimpleDateFormat _dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String _beginTimeStr = _dateFormat.format(t.getBegintime());

                List<CrmTaskProjectRelation> _projectRelations = _taskRepository.getTaskProjectRelation(_taskid);
                if (_projectRelations != null && _projectRelations.size() > 0) {
                    _messageTemplate = String.format("请尽快完成任务：%s，开始时间：%s", _taskContent, _beginTimeStr);
                }
                String _message = String.format(_messageTemplate, t.getContent());
                String _title = _taskContent;

                List<CrmTaskMember> _members = _taskRepository.getTaskMembers(_taskid);
                List<String> _userids = new ArrayList<>();
                for (CrmTaskMember m : _members) {
                    _userids.add(Integer.toString(m.getUserid()));
                }

                Collection<String> alias = new LinkedList<String>();
                List<CrmUser> _users = _friendRepository.getUserInfoList(_userids);
                for (CrmUser u : _users) {
                    alias.add(u.getUsername());
                }

                JPushCustomerContent cc = new JPushCustomerContent();
                cc.setKey("e1117369b0a39e42185042aa");
                cc.setProjectid("3a95002c4cfe848816fc18db");
                cc.setPushType("taskAlert");
                cc.setTaskid(_taskid);

                //极光推送
                System.out.println("alias:" + alias.toString());
                System.out.println("_title:" + _title);
                System.out.println("_messageTemplate:" + _messageTemplate);
                System.out.println("cc" + cc);
                _jPushService.sendPushByAliasCustomer(alias, _title, _messageTemplate, cc);
                //信鸽推送
                _xingeService.pushMessage(_taskid, "", _userids, _title, _message);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }



    public void execute1() {

        System.out.println("the 24 hour job ");

        try {
            String _messageTemplate = "请尽快完成本任务";
            Collection<String> alias = new LinkedList<String>();
            String _title = "测试任务";

            JPushCustomerContent cc = new JPushCustomerContent();
            cc.setKey("e1117369b0a39e42185042aa");
            cc.setProjectid("3a95002c4cfe848816fc18db");
            cc.setPushType("taskAlert");
            cc.setTaskid("1");

            //极光推送
            alias.add("17638512031");
            System.out.println("alias:" + alias.toString());
            System.out.println("_title:" + _title);
            System.out.println("_messageTemplate:" + _messageTemplate);
            System.out.println("cc" + cc);
            _jPushService.sendPushByAliasCustomer(alias, _title, _messageTemplate, cc);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
